package dao;

import pojo.News;
import util.JDBCUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NewsDaoImp implements NewsDao{
    @Override
    public Integer selsectNewsCount(Map<String, Object> params) {
        String sql = "select count(*) from news";
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.excuteQuery(sql);
        try {
            if (resultSet.next()){
                return resultSet.getInt(1);
            }else {
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            jdbcUtil.close();
        }
    }

    private String createCondition(Map<String,Object> params){
        StringBuffer buffer = new StringBuffer();
        String title = (String) params.get("title");
        if (title != null && !title.equals("")){
            buffer.append("and n.title like '%" + title + "%' ");
        }
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        if (beginTime != null && !beginTime.equals("")){
            buffer.append("and date_format(time,'%Y-%m-%d') >= '" + beginTime + "' ");
            if (endTime != null && !endTime.equals("")){
                buffer.append("and date_format(time,'%Y-%m-%d') >= '" + beginTime + "' and date_format(time,'%Y-%m-%d') <= '" + endTime +"' ");
            }
        }else if (endTime != null && !endTime.equals("")){
            buffer.append("and date_format(time,'%Y-%m-%d') <= '" + endTime +"' ");
        }
        return buffer.toString();
    }

    @Override
    public List<News> selectAllNews(Map<String,Object> params) {
        List<News> news = new ArrayList<>();
        String sql = "SELECT n.*, w.name FROM news n , writer w where n.wId = w.writerId ";
        Integer start = (Integer) params.get("start");
        System.out.println(start);
        Integer length = (Integer) params.get("length");
        System.out.println(length);
        sql += createCondition(params);
        sql += "order by n.id desc limit ?, ?";
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.excuteQuery(sql, start, length);
        try {
            while (resultSet.next()){
                News news1 = new News();
                news1.setId(resultSet.getInt("id"));
                news1.setTitle(resultSet.getString("title"));
                news1.setTime(resultSet.getDate("time"));
                news1.setContent(resultSet.getString("content"));
                news1.setViews(resultSet.getInt("views"));
                news1.setWriterId(resultSet.getInt("wId"));
                news1.setName(resultSet.getString("name"));
                news.add(news1);
            }

            return news;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            jdbcUtil.close();
        }

    }

    @Override
    public News selectNewsById(Integer newsId) {
        String sql = "select n.*, w.name from news n inner join writer w on n.wid = w.writerId where n.id=?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = jdbcUtil.excuteQuery(sql, newsId);
        System.out.println(sql);
        News news = null;
        try {
            if (resultSet.next()){
                news = new News();
                news.setId(resultSet.getInt("id"));
                news.setTitle(resultSet.getString("title"));
                news.setTime(resultSet.getDate("time"));
                news.setContent(resultSet.getString("content"));
                news.setViews(resultSet.getInt("views"));
                news.setWriterId(resultSet.getInt("wId"));
                news.setName(resultSet.getString("name"));
            }
            return news;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public Integer updateViewsById(News news) {
        String sql = "update news set views = ? where id = ? ";
        System.out.println(sql);
        JDBCUtil jdbcUtil = new JDBCUtil();
        Integer integer = jdbcUtil.excuteUpdate(sql,false,news.getViews(),news.getId());
        return  integer;
    }
}
